package de.herberlin.pss.action;

import org.apache.log4j.Logger;

import de.herberlin.pss.ServiceCallback;
import de.herberlin.pss.ServiceLocator;
import de.herberlin.pss.ui.SongTableModel;

/**
 * Fill some Tables with default data required by the application
 * 
 * @author aherbertz
 * 
 */
public class DefaultDataAction extends AbstractProgressDialogAction {

	public void doPerform() {
		try {
			createDummyDirTabledata();
			createDummyAlbumTabledata();
			createDummyGenreTabledata();
			createDummyPlaylist();
		} catch (Exception e) {
			Logger.getLogger(getClass()).error(e, e);
		} finally {
			SongTableModel.getInstance().updateContent();
		}

	}

	public static final int DIR_ID = 4;
	public static final int ALBUM_ID = 99;
	public static final int GENRE_ID = 99;
	public static final int PLAYLIST_ID = 99;

	private void createDummyDirTabledata() throws Exception {
		String filename = ServiceLocator.getSqliteFile().getAbsolutePath();
		new ServiceCallback(filename).getResult("delete from dirTable");
		new ServiceCallback(filename)
				.getResult("INSERT INTO dirTable VALUES(1,'C:',-559030611)");
		new ServiceCallback(filename)
				.getResult("INSERT INTO dirTable VALUES(2,'_system',1);");
		new ServiceCallback(filename)
				.getResult("INSERT INTO dirTable VALUES(3,'media',2);");
		new ServiceCallback(filename)
				.getResult("INSERT INTO dirTable VALUES(4,'audio',3);");
	}

	private void createDummyAlbumTabledata() throws Exception {
		String filename = ServiceLocator.getSqliteFile().getAbsolutePath();
		new ServiceCallback(filename)
				.getResult("delete from albumTable where iAlbumId=" + ALBUM_ID);
		new ServiceCallback(filename)
				.getResult("INSERT INTO albumTable VALUES(" + ALBUM_ID
						+ ",'Album not supported.')");
	}

	private void createDummyGenreTabledata() throws Exception {
		String filename = ServiceLocator.getSqliteFile().getAbsolutePath();
		new ServiceCallback(filename)
				.getResult("delete from genreTable where iGenreId=" + GENRE_ID);
		new ServiceCallback(filename)
				.getResult("INSERT INTO genreTable VALUES(" + GENRE_ID
						+ ",'Genre not supported');");
	}

	private void createDummyPlaylist() throws Exception {
		String filename = ServiceLocator.getSqliteFile().getAbsolutePath();
		new ServiceCallback(filename)
				.getResult("delete from playlistTable where iPlaylistId="
						+ PLAYLIST_ID);
		new ServiceCallback(filename)
				.getResult("INSERT INTO playlistTable VALUES(" + PLAYLIST_ID
						+ ",'Default Playlist',null,0);");
	}

	public void doRollback() {
	}

}
